package com.aztec.ims.service.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

import com.aztec.ims.domain.TaxScheme;
import com.aztec.ims.service.om.TaxingScheme;
import com.aztec.ims.service.response.GenericResponse;
import com.aztec.ims.service.util.HibernateUtil;

public class TaxSchemeDao {
	public GenericResponse createTaxScheme(TaxScheme request) {
		TaxingScheme om = convertRequestToOm(request);

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(om);
		session.getTransaction().commit();

		GenericResponse response = new GenericResponse(om.getTaxschemeId()
				.toString());
		return response;
	}

	public List<TaxScheme> getTaxSchemes() {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(TaxingScheme.class);
		criteria.addOrder(Order.asc("taxschemeId"));
		List results = criteria.list();
		session.getTransaction().commit();

		return convertOmToResponse(results);

	}

	private List<TaxScheme> convertOmToResponse(List results) {
		List<TaxScheme> response = new ArrayList<TaxScheme>();

		if (results != null) {
			for (Iterator iter = results.iterator(); iter.hasNext();) {
				TaxingScheme om = (TaxingScheme) iter.next();
				TaxScheme domain = new TaxScheme();
                domain.setTaxschemeId(om.getTaxschemeId().toString());
                domain.setTaxschemeName(om.getTaxschemeName());
                domain.setActiveFlag(convertCharacterToBoolean(om.getActiveFlag()));
                domain.setCompoundFlag(convertCharacterToBoolean(om.getCompoundFlag()));
                domain.setPrimTaxName(om.getPrimTaxName());
                domain.setPrimTaxRate(om.getPrimTaxRate());
                domain.setSecTaxName(om.getSecTaxName());
                domain.setSecTaxRate(om.getSecTaxRate());
                domain.setTaxShipFlag(convertCharacterToBoolean(om.getTaxShipFlag()));
				response.add(domain);
			}
		}

		return response;
	}

	private TaxingScheme convertRequestToOm(TaxScheme request) {

		TaxingScheme om = new TaxingScheme();
		om.setActiveFlag(convertBooleanToCharacter(request.getActiveFlag()));
		om.setCompoundFlag(convertBooleanToCharacter(request.getCompoundFlag()));
		om.setPrimTaxName(request.getPrimTaxName());
		om.setPrimTaxRate(request.getPrimTaxRate());
		om.setSecTaxName(request.getSecTaxName());
		om.setSecTaxRate(request.getSecTaxRate());
		om.setTaxschemeName(request.getTaxschemeName());
		om.setTaxShipFlag(convertBooleanToCharacter(request.getTaxShipFlag()));

		return om;
	}

	private Character convertBooleanToCharacter(Boolean from) {
		Character to = null;
		if (from != null) {
			if (from.booleanValue()) {
				to = new Character('Y');
			} else {
				to = new Character('N');
			}
		}
		return to;
	}

	private Boolean convertCharacterToBoolean(Character from) {
		Boolean to = null;
		if (from != null) {
			if (from.charValue()=='Y') {
				to = new Boolean(true);
			} else {
				to = new Boolean(false);
			}
		}
		return to;
	}

}
